<template>
  <div class="inventory_list">
    <!--库存列表操作 -->
    <el-row class="inventory_operation">
      <el-col :span="20">
        <div class="title">库存列表</div>
      </el-col>
      <el-col :span="4">
        <el-tooltip class="item" effect="light" content="出库" placement="top">
          <el-button type="primary" circle @click="showInventoryPopus" class="iconfont icon-chuku purchaseBtn"></el-button>
        </el-tooltip>
      </el-col>
    </el-row>
    <!-- 库存列表 -->
    <el-table
        :data="inventoryList"
        border
        @selection-change="selectTarget"
    >
      <!-- 全选 -->
      <el-table-column type="selection" width="40"/>
      <!-- 展开行 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-card>
            <!-- 药品信息 -->
            <el-divider content-position="left">药品信息</el-divider>
            <div class="description">类别描述：{{scope.row.medicine.medicineTypeVo.description}}</div>
            <div class="specification">规格：{{scope.row.medicine.specification}}</div>
            <div class="efficacy">作用：{{scope.row.medicine.efficacy}}</div>
            <el-row>
              <el-col :span="4">
                <div class="purchasePrice">进货价格：{{scope.row.medicine.purchasePrice}}元/盒</div>
              </el-col>
              <el-col :span="4">
                <div class="retailPrice">零售价格：{{scope.row.medicine.retailPrice}}元/盒</div>
              </el-col>
              <el-col :span="4">
                <div class="level">库存等级：{{scope.row.medicine.medicineTypeVo.level}}</div>
              </el-col>
              <el-col :span="4">
                是否有医保：
                <el-tag :type="scope.row.medicine.medicalInsurance == '医保' ? '' : 'info'">{{ scope.row.medicine.medicalInsurance }}</el-tag>
              </el-col>
              <el-col :span="4">
                是否为捐赠物资：
                <el-tag :type="scope.row.medicine.donate == '捐赠' ? 'success' : 'info'">{{ scope.row.medicine.donate }}</el-tag>
              </el-col>
            </el-row>
          </el-card>
        </template>
      </el-table-column>
      <!-- 药品图片 -->
      <el-table-column prop="medicine" label="药品图片" width="150" align="center">
        <template slot-scope="scope">
          <img :src="'http://47.107.237.85/mdps/img/'+scope.row.medicine.img" alt="图片加载错误">
        </template>
      </el-table-column>
      <!-- 药品名称 -->
      <el-table-column prop="medicine.name" label="药品名称" width="150" align="center"/>
      <!-- 供应商 -->
      <el-table-column prop="supplier" label="供应商" width="170px" align="center"/>
      <!-- 仓库 -->
      <el-table-column prop="warehouse" label="仓库" width="120px" align="center"/>
      <!-- 药品类别 -->
      <el-table-column prop="medicine.medicineTypeVo.type" label="药品类别" width="90px" align="center"/>
      <!-- 生产日期 -->
      <el-table-column prop="medicine.manufactureData" label="生产日期" width="110px" align="center"/>
      <!-- 有效日期 -->
      <el-table-column prop="medicine.expirationDate" label="有效日期" width="110px" align="center"/>
      <!-- 库存数量 -->
      <el-table-column prop="purchaseNumber" label="库存数量" width="100px" align="center"/>
      <!-- 出库数量 -->
      <el-table-column prop="deliverNumber" label="出库数量" width="100px" align="center"/>
      <!-- 现有数量 -->
      <el-table-column prop="existedNumber" label="现有数量" align="center"/>
    </el-table>
    <!-- 库存分页 -->
    <el-pagination
        @size-change="handleInventoryTableSizeChange"
        @current-change="handleInventoryTableCurrentChange"
        :current-page="inventoryPageList.currentPage"
        :page-sizes="[5, 10, 15, 20, 30]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="inventoryTotal">
    </el-pagination>
    <!-- 出库弹出层 -->
    <el-dialog  title="出库药品信息" :visible.sync="showInventory" width="65%" @close="handleCloseInventory" top="10vh" append-to-body>
        <el-form inline :model="inventoryForm" ref="inventoryFormRef" :rules="inventoryFormRules">
            <!-- 出库数量 -->
            <el-form-item prop="number">
              <el-input placeholder="请输入出库数量" v-model.number="inventoryForm.number" clearable>
                <template slot="prepend">出库数量</template>
              </el-input>
            </el-form-item>
            <el-divider content-position="left">请填写第三方信息</el-divider>
            <!-- 购买单位 -->
            <el-form-item prop="buyer">
                <el-input placeholder="请输入购买单位" v-model="inventoryForm.buyer" clearable>
                  <template slot="prepend">购买单位</template>
                </el-input>
            </el-form-item>
            <!-- 联系电话 -->
            <el-form-item prop="contactNumber">
              <el-input placeholder="请输入第三方联系电话" v-model="inventoryForm.contactNumber" clearable>
                    <template slot="prepend">第三方联系电话</template>
                </el-input>
            </el-form-item>
            <!-- 地址 -->
            <el-form-item prop="address">
              <el-cascader v-model="addressSelected" clearable placeholder="请选择第三方地址" :options="addressDownBox"
                :props="{ expandTrigger: 'hover' }" @change="handleAddressChange">
              </el-cascader>
            </el-form-item>
            <!-- 详细地址 -->
            <el-form-item prop="detailAddress">
                <el-input 
                  class="detailAddress"
                  clearable
                  placeholder="请输入详细地址" 
                  type="textarea" 
                  :autosize="{ minRows: 4, maxRows: 10}" 
                  v-model="inventoryForm.detailAddress"
                >
                </el-input>
            </el-form-item>
            <!-- 备注 -->
            <el-form-item prop="remark">
                <el-input 
                  class="remark"
                  clearable
                  placeholder="请输入备注" 
                  type="textarea" 
                  :autosize="{ minRows: 4, maxRows: 10}" 
                  v-model="inventoryForm.remark"
                >
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer">
            <el-button @click="showInventory = false">取 消</el-button>
            <el-button type="primary" @click="inventory">确 认</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'inventoryList',
    data() {

      /* 手机号表单验证 */
      var checkPhone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('练习电话不能为空'));
        } else {
          const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
          if (reg.test(value)) {
            callback();
          } else {
            return callback(new Error('请输入正确的手机号'));
          }
        }
      };

      return {
        /* 库存列表 */
        inventoryList: [],
        /* 库存列表表分页信息 */
        inventoryPageList:{
          pageNo: 1,
          pageSize: 5,
          currentPage: 1,
          medicine: '',
          supplier: '',
          warehouse: '',
          purchaseNumber: '',
          deliverNumber: '',
          existedNumber: '',
        },
        /* 库存表总条数 */
        inventoryTotal: 0,

        /* 出库弹出层 */
        showInventory: false,
        /* 出库药品表单 */
        inventoryForm: {
          currentUserId: '',
          medicine: '',
          number: '',
          retailPrice: '',
          supplierId: '',
          warehouseId: '',
          aggregateAmount: '',
          buyer: '',
          contactNumber: '',
          remark: '',
          address: '',
          detailAddress: '',
        },
        /* 出库药品表单验证 */
        inventoryFormRules: {
          number: [
            { required: true, message: '请输入采购药品数量', trigger: 'blur' },
            { type: 'number', message: '出库数量必须为数字'}
          ],
        },

        /* 选中库存列表个数 */
        selectedInventoryNumber: 0,
        /* 选中库存列表数组 */
        selectedInventory: [],

        /* 选中的地址 */
        addressSelected: '',
        /* 地址下拉框数据 */
        addressDownBox: [],
      }
    },
    methods: {
      /* 复选框选择目标事件回调 */
      selectTarget(selection) {
          this.selectedInventoryNumber =  selection.length
          this.selectedInventory = selection
      },

      /* 分页获得库存列表 */
      async getInventoryPageList(inventory) {

        if(inventory != undefined) {
          this.inventoryPageList.supplier = inventory.supplier
          this.inventoryPageList.warehouse = inventory.warehouse
          this.inventoryPageList.purchaseNumber = inventory.purchaseNumber
          this.inventoryPageList.deliverNumber = inventory.deliverNumber
          this.inventoryPageList.existedNumber = inventory.existedNumber
        }

        const { data: res } = await this.$http.post('workbench/inventory/inventoryPageList.do',this.inventoryPageList)
        if (res.code == '1') {
          this.inventoryList = res.others
        } else {
          this.$message.error('获取库存列表表失败!')
        }
      },
      /* 获得库存列表列表总条数 */
      async getInventoryTotal() {
          const { data: res } = await this.$http.post('workbench/inventory/getInventoryTotal.do',this.inventoryPageList)
          if (res.code == '1') {
            this.inventoryTotal = res.number
          } else {
              this.$message.error('获取目标表总条数失败!')
          }
      },
      /* 处理分页每页显示几行 */
      handleInventoryTableSizeChange(val) {
          this.inventoryPageList.pageSize = val
          this.getInventoryTotal()
          this.getInventoryPageList()
      },
      /* 处理分页切换页数 */
      handleInventoryTableCurrentChange(val) {
          this.inventoryPageList.pageNo = val
          this.getInventoryTotal()
          this.getInventoryPageList()
      },
      
      /* 出库药品弹出层 */
      showInventoryPopus() {
        if(this.selectedInventoryNumber > 1) {
            this.$message.error('只能选择一条药品进行出库!')
            return
        }
        if(this.selectedInventoryNumber < 1) {
            this.$message.error('请选择需要出库的药品!')
            return
        }

        this.inventoryForm.medicine = this.selectedInventory[0].medicine.id
        this.inventoryForm.supplierId = this.selectedInventory[0].supplierId
        this.inventoryForm.warehouseId = this.selectedInventory[0].warehouseId
        this.inventoryForm.retailPrice = this.selectedInventory[0].medicine.retailPrice

        this.showInventory = true
      },
      
      /* 出库药品 */
      async inventory() {
        this.$refs.inventoryFormRef.validate(async valid => {
          if (!valid) return

          this.inventoryForm.aggregateAmount = this.inventoryForm.number * this.inventoryForm.retailPrice

          console.log(this.inventoryForm)
          const { data: res } = await this.$http.post('workbench/inventory/deliver.do',this.inventoryForm)
          if (res.code == '1') {
            this.showInventory = false
            this.$refs['inventoryFormRef'].resetFields();
            this.$message.success('出库成功!')
          } else {
            this.$message.error('出库失败!')
          }
        })
      },
      /* 出库药品弹出层关闭回调 */
      handleCloseInventory() {
        this.$refs['inventoryFormRef'].resetFields();
      },

      /* 获得地址下拉框数据 */
      async getAddressData() {
          const { data: res } = await this.$http.get('settings/provinceCity/getAddressData.do')
          if (res.code == '1') {
            this.addressDownBox = res.others
          } else {
              console.log('获取地址下拉框数据失败!')
          }
      },

      /* 添加员工修改地址事件回调 */
      handleAddressChange(value) {
        const address = value[0] + " · " + value[1]
        this.inventoryForm.address = address
      },

      /* 初始化当前登录用户 */
      getCurrentUser() {
        let user = JSON.parse(window.localStorage.getItem('user'))
        this.inventoryForm.currentUserId = user.id
      },  
    },
    created() {
      this.getCurrentUser()
      this.getInventoryTotal()
      this.getInventoryPageList()
      this.getAddressData()
    },
    mounted() {
      this.$bus.$on('getInventoryPageList',this.getInventoryPageList)
      this.$bus.$on('getInventoryTotal',this.getInventoryTotal)
    },
    beforeDestroy() {
      this.$bus.$off('getInventoryPageList')
      this.$bus.$off('getInventoryTotal')
    }
  }
</script>

<style scoped>
/* 库存列表列表 */
.inventory_list {
  margin: 0px 20px;
  box-shadow: 0 0 10px rgb(220, 223, 220);
}
/* 库存列表列表操作 */
.inventory_operation {
  background-color: #fff;
  margin: 0px 0px;
  height: 70px;
}
.inventory_operation .title {
  line-height: 70px;
  margin-left: 15px;
  font-weight: bold;
}
.inventory_operation .el-button {
  margin-top: 15px;
}
/* 出库按钮 */
.purchaseBtn {
  float: right;
  margin-right: 50px;
}
/* 药品图片 */
.el-table img {
  width: 120px;
}
/* 药品卡片展示 */
.el-table .el-card {
  margin: 10px 30px;
}
.el-table .el-card .purchasePrice,.retailPrice,.level{
  line-height: 30px;
}
.el-table .el-card .description,.specification,.efficacy{
  margin-bottom: 15px;
}
/* 库存列表分页 */
.el-pagination {
  text-align: center;
  padding: 15px 0px;
  background-color: #fff;
}
.remark,.detailAddress {
  width: 400px;
}
</style>
